#include "GLPrimitives.h"
#include "ClippingPlane.h"
#include "General Utils.hpp"
#include "GameWorld.h"

const float DEG2RAD = (3.141590f/180.0f);

GLPrimitives* GLPrimitives::Instance()
{
	static GLPrimitives instance;
	return &instance;
	
		
}

GLPrimitives::GLPrimitives()
{
}

void GLPrimitives::DrawRectangle(int x1, int y1, int x2, int y2, OGL_Color color)
{
	GameRectangle aRect = GetRect(x1, y1, x2, y2);

	if(/*ClipPlane->IsInPlane(aRect)*/1)
	{

		glDisable(GL_TEXTURE_2D);

		// Custom color
		glColor4f(color.r, color.g, color.b, color.a);

		GameRectangle aRect = GetRect(x1, y1, x2, y2);

		glBegin(GL_QUADS);
			glVertex3f( GLfloat(aRect.x), GLfloat(aRect.y), 1.0f );
			glVertex3f( GLfloat(aRect.x + aRect.width), GLfloat(aRect.y), 1.0f );
			glVertex3f( GLfloat(aRect.x + aRect.width), GLfloat(aRect.y + aRect.height), 1.0f );
			glVertex3f( GLfloat(aRect.x), GLfloat(aRect.y + aRect.height), 1.0f );
		glEnd();

		glEnable(GL_TEXTURE_2D);
	}
}



void GLPrimitives::DrawRectangle(int x1, int y1, int x2, int y2, OGL_Color color, float alpha, float rotation)
{
	GameRectangle aRect = GetRect(x1, y1, x2, y2);;

	GameRectangle aRect2;
	aRect2.x = x1 + ClipPlane->GetX();
	aRect2.y = y1 + ClipPlane->GetY();
	aRect2.width = x2 - x1;
	aRect2.height = y2 - y1;

	if(ClipPlane->IsInPlane(aRect) || ClipPlane->IsInPlane(aRect2))
	{

		glDisable(GL_TEXTURE_2D);

		// Custom color
		glColor4f(color.r, color.g, color.b, alpha);

		glPushMatrix();
		glRotatef(rotation,  0.0f, 0.0f, 1.0f);


		glBegin(GL_QUADS);
			glVertex3f( GLfloat(aRect.x), GLfloat(aRect.y), 1.0f );
			glVertex3f( GLfloat(aRect.x + aRect.width), GLfloat(aRect.y), 1.0f );
			glVertex3f( GLfloat(aRect.x + aRect.width), GLfloat(aRect.y + aRect.height), 1.0f );
			glVertex3f( GLfloat(aRect.x), GLfloat(aRect.y + aRect.height), 1.0f );
		glEnd();

		glPopMatrix();

		glEnable(GL_TEXTURE_2D);
	}
}



void GLPrimitives::DrawLine(int x1, int y1, int x2, int y2, OGL_Color color)
{
	glDisable(GL_TEXTURE_2D);
	glColor4f(color.r, color.g, color.b, 1.0f);

	glBegin(GL_LINES);
		glVertex3f(GLfloat(x1), GLfloat(y1), 0.0f);
		glVertex3f(GLfloat(x2), GLfloat(y2), 0.0f);
	glEnd();

	glEnable(GL_TEXTURE_2D);
}


void GLPrimitives::DrawCircle(int x, int y, int r, OGL_Color color)
{
	

	GameRectangle aRect;
	aRect.x = x - r + ClipPlane->GetX();
	aRect.y = y - r + ClipPlane->GetY();
	aRect.width = 2 * r;
	aRect.height = 2 * r;

	if(ClipPlane->IsInPlane(aRect))
	{

		// Disable Textures so circles show up
		glDisable(GL_TEXTURE_2D);

		glPushMatrix();

		glTranslatef(x, y, 0.0f); // translate to correct position
		glColor4f(color.r, color.g, color.b, color.a);

		glBegin(GL_LINE_LOOP);
			for (int i=0; i < 360; i++)
			{
			  float degInRad = i*DEG2RAD;
			  glVertex2f(cos(degInRad)*r,sin(degInRad)*r); // Loop through 360 points
			}
		glEnd();

		glPopMatrix();

		// Reenable textures
		glEnable(GL_TEXTURE_2D);

	}
}

void GLPrimitives::DrawPolygon(std::vector<Vector2D> points)
{
	glDisable(GL_TEXTURE_2D);
	glColor4f(0.5f, 0.5f, 0.5f, 0.5f);

	glBegin(GL_POLYGON);
		for(int i = 0; i < points.size(); i++)
			glVertex3f(points[i].x - ClipPlane->GetX(), points[i].y - ClipPlane->GetY(), 0.0f);
	glEnd();
	glEnable(GL_TEXTURE_2D);
}